import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:homemedia/model/media.dart';
import 'package:homemedia/page/tv/home/video_card.dart';

class VideoScrollView extends StatelessWidget {

  final String title;
  final List<MediaLibrary> librarylist;
  final ValueNotifier<int> _videoCount = ValueNotifier<int>(0);
  VideoScrollView({super.key, required this.title, required this.librarylist});

  @override
  Widget build(BuildContext context) {

    // var ch = <MediaLibrary>[];
    // for (var i = 0; i < 15; i++) {
    //   // ch.add(Text('点击了 $i 次'));
    //   ch.add(MediaLibrary());
    // }
    _videoCount.value = librarylist.length;
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ValueListenableBuilder<int>(
            valueListenable: _videoCount,
            builder: (context, videoCount, _) {
              return _SectionHeader(
                title: title??"",
                hideSeeAll: videoCount < 10,
              );
            },
          ),
          const SizedBox(height: 10.0),
          SizedBox(
            height: 250,
            child: ListView.builder(
              padding: const EdgeInsets.symmetric(horizontal: 15.0),
              scrollDirection: Axis.horizontal,
              itemCount: librarylist.length,
              shrinkWrap: true,
              itemBuilder: (BuildContext context, int index) {
                final MediaLibrary entry = librarylist[index];
                return Padding(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 5.0,
                    vertical: 10.0,
                  ),
                  child: VideoCard(img: "http://192.168.31.221:8096/Items/00f4e72f6c222cd20f10e5f9771cfabf/Images/Primary?fillHeight=401&fillWidth=711&quality=96&tag=2f0134150e78120f98392abee502e422", entry: entry),
                );
              },
            ),
          )
        ]
    );
  }

}

class _SectionHeader extends StatelessWidget {
  // final Link link;
  final String title;
  final bool hideSeeAll;

  const _SectionHeader({required this.title, this.hideSeeAll = false});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Flexible(
            child: Text(
              '$title',
              style: const TextStyle(
                fontSize: 20.0,
                fontWeight: FontWeight.w500,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
          if (!hideSeeAll)
            GestureDetector(
              onTap: () {
                // context.router.push(
                //   GenreRoute(title: '${link.title}', url: link.href!),
                // );
              },
              child: const Text(
                'See All',
                style: TextStyle(
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
        ],
      ),
    );
  }
}